/*
 * @Author: sbotlp
 * @Email: sbotlp@sina.com
 * @Date: 2019-06-29 17:45:18
 * @LastEditTime: 2020-09-02 11:16:33
 * @Description: http://www.sbotlp.com.cn
 */
import * as React from 'react';
import { Button } from 'antd';
import { withRouter } from 'src/app/routers/WithRouter';
import { connect } from 'src/app/redux/connect/Connect';
import { openLoading, closeLoading } from 'src/app/redux/actions/Loading.actions';
import { addBulletConstants, bachAddBulletConstants} from 'src/app/redux/actions/Bullet.actions';
import { Card } from '../component/card/Card';
import { HomeService } from './Home.service';
import './Home.scss';
interface Props extends React.Props<any> {
  type?: string | number;
  history?: any;
  Bullet?: any;
  Loading?: any;
  addBulletConstants?: () => void;
  bachAddBulletConstants?: () => void;
  openLoading?: () => void;
  closeLoading?: () => void;
}
@withRouter
@connect(
  state => state,
  { addBulletConstants, bachAddBulletConstants, openLoading, closeLoading }
)
export default class HomeComponent extends React.PureComponent<Props, any> {
  private time: number;
  private service: HomeService;
  constructor(props: Props) {
    super(props);
    this.service = new HomeService();
  }
  componentDidMount = () => {
    this.service.save({}).then(res => {
      console.log('res: ', res);
    })
    this.service.notRes({}).then(res => {
      console.log('res: ', res);
    })
  }
  go = (patn) => {
    this.props.history.push(patn);
  }
  add = () => {
    this.props.addBulletConstants!();
  }
  batchAdd = () => {
    this.props.bachAddBulletConstants!();
  }
  opLoading = () => {
    const { open } = this.props.Loading;
    if (open) {
      this.props.closeLoading!();
    }
    this.props.openLoading!();
    this.time = setTimeout(() => {
      this.props.closeLoading!();
      clearInterval(this.time);
    }, 1000)
  }
  render() {
    return (
      <div className='home'>
        <Card title={<span className='title'>添加子弹</span>}>添加子弹</Card>
        <Card>
          <Button type='primary' onClick={() => this.go('/login/index')}>
            去消耗子弹
          </Button>
        </Card>
        <Card>
          <Button type='primary' onClick={() => this.go('/form/index')}>
            去表单演示
          </Button>
        </Card>
        <Card>当前子弹数量： {this.props.Bullet.bulletNumber}</Card>
        <Card>
          <Button type='primary' onClick={this.add}>
            添加子弹
          </Button>
        </Card>
        <Card>
          <Button type='primary' onClick={this.batchAdd}>
            批量添加子弹
          </Button>
        </Card>
        <Card>
          <Button type='primary' onClick={this.opLoading}>
            打开Loading
          </Button>
        </Card>
      </div>
    )
  }
}